package com.liouwb.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;

/**
 * @author liouwb
 */
@RestController
@RequestMapping("consumer")
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private WebClient.Builder clientBuilder;

    @GetMapping("getProvider")
    public String getProvider() {
//        return restTemplate.getForObject("http://localhost:8001/provider/getProvider", String.class);
        // 使用ribbin做负载均衡，ip采用yml里面配置的application名称
        return restTemplate.getForObject("http://privider-server/provider/getProvider", String.class);
    }

    /**
     * 添加hystrix的方法
     *
     * @return
     */
    @GetMapping("getProviderHystrix")
    public String getProviderHystrix() {
        return restTemplate.getForObject("http://privider-server/provider/getProviderHystrix", String.class);
    }

    /**
     * webclient使用ribbon实现负载均衡
     *
     * @return
     */
    @GetMapping("webClientLoad")
    public String webClientLoad() {
        return clientBuilder
                .baseUrl("http://privider-server/provider/getProvider")
                .build()
                .method(HttpMethod.GET)
                .retrieve()//请求结果的方法
                .bodyToMono(String.class)
                .block();
    }
}
